//
//  SFMainViewController.m
//  SFNavigationBarManager
//
//  Created by yangshen on 11/17/2021.
//  Copyright (c) 2021 yangshen. All rights reserved.
//

#import "SFMainViewController.h"
#import "SFSettingsViewController.h"
#import "SFNavigationBarData.h"

@interface SFMainViewController ()

@property (nonatomic, strong) SFNavigationBarData *nextBarData;

@property (weak, nonatomic) IBOutlet UILabel *barTintLabel;
@property (weak, nonatomic) IBOutlet UILabel *barImageLabel;
@property (weak, nonatomic) IBOutlet UISwitch *navBarSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *shadownImageSwitch;

@end

@implementation SFMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"Demo-%zd", self.navigationController.viewControllers.count];

    if (!self.navBarData) {
        UISwitch *switchView = [[UISwitch alloc] init];
        switchView.on = YES;
        [switchView addTarget:self action:@selector(toggleTranslucent:) forControlEvents:UIControlEventValueChanged];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:switchView];
        self.navigationController.navigationBar.translucent = YES;
    }

    self.barTintLabel.text = [SFNavigationBarData nameWithStyle:self.navBarData.barColorIndex] ?: @"White";
    self.barImageLabel.text = [SFNavigationBarData nameWithStyle:self.navBarData.barImageIndex] ?: @"White";
    self.navBarSwitch.on = self.navBarData.navBarHidden;
    self.shadownImageSwitch.on = self.navBarData.shaowImageHidden;

    if (@available(iOS 15, *)) {
        UINavigationBarAppearance *navigationBarAppearance = [[UINavigationBarAppearance alloc] init];
        if (self.navigationController.navigationBar.isTranslucent) {
            [navigationBarAppearance configureWithTransparentBackground];
        } else {
            [navigationBarAppearance configureWithOpaqueBackground];
        }

        navigationBarAppearance.backgroundColor = [SFNavigationBarData barTintColorWithStyle:self.navBarData.barColorIndex];
        navigationBarAppearance.backgroundImage = [SFNavigationBarData barImageWithStyle:self.navBarData.barImageIndex];
        navigationBarAppearance.shadowImage = self.navBarData.shaowImageHidden ? [UIImage new] : nil;
        self.navigationController.navigationBar.standardAppearance = navigationBarAppearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = navigationBarAppearance;
    } else {
        self.navigationController.navigationBar.barTintColor = [SFNavigationBarData barTintColorWithStyle:self.navBarData.barColorIndex];
        [self.navigationController.navigationBar setBackgroundImage:[SFNavigationBarData barImageWithStyle:self.navBarData.barImageIndex] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = self.navBarData.shaowImageHidden ? [UIImage new] : nil;
    }
}

- (IBAction)toggleNavBarHiddenAction:(UISwitch *)sender {
    self.nextBarData.navBarHidden = sender.isOn;
}

- (IBAction)toggleNavBarShaowImageHiddenAction:(UISwitch *)sender {
    self.nextBarData.shaowImageHidden = sender.isOn;
}


- (void)toggleTranslucent:(UISwitch *)sender {
    self.navigationController.navigationBar.translucent = sender.isOn;
}

- (IBAction)pushNextAction {
    SFMainViewController *mainVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"MainViewController"];
    mainVC.navBarData = self.nextBarData;
    [self.navigationController pushViewController:mainVC animated:YES];
}

- (IBAction)popAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)popTo_2Action:(id)sender {
    if (self.navigationController.viewControllers.count > 3) {
        UIViewController *vc = self.navigationController.viewControllers[self.navigationController.viewControllers.count-3];
        [self.navigationController popToViewController:vc animated:YES];
    }
}

- (IBAction)popToRootAction:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (SFNavigationBarData *)nextBarData {
    if (!_nextBarData) {
        _nextBarData = [[SFNavigationBarData alloc] init];
        if (self.navBarData) {
            _nextBarData.barColorIndex = self.navBarData.barColorIndex;
            _nextBarData.barImageIndex = self.navBarData.barImageIndex;
            _nextBarData.navBarHidden = self.navBarData.navBarHidden;
            _nextBarData.shaowImageHidden = self.navBarData.shaowImageHidden;
        } else {
            _nextBarData.barColorIndex = 0;
            _nextBarData.barImageIndex = 0;
            _nextBarData.navBarHidden = NO;
            _nextBarData.shaowImageHidden = NO;
        }
    }
    return _nextBarData;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section > 0 || indexPath.row > 1) {
        return;
    }

    SFSettingsViewController *vc = [[SFSettingsViewController alloc] init];
    __weak typeof(self)weakSelf = self;
    if (indexPath.row == 0) {
        vc.selectBarTintColor = YES;
        vc.selectIndex = self.nextBarData.barColorIndex;
        vc.selectIndexBlok = ^(NSInteger index) {
            weakSelf.nextBarData.barColorIndex = index;
            weakSelf.barTintLabel.text = [SFNavigationBarData nameWithStyle:index];
        };

    } else if (indexPath.row == 1) {
        vc.selectBarTintColor = NO;
        vc.selectIndex = self.navBarData.barImageIndex;
        vc.selectIndexBlok = ^(NSInteger index) {
            weakSelf.nextBarData.barImageIndex = index;
            weakSelf.barImageLabel.text = [SFNavigationBarData nameWithStyle:index];
        };
    }
    [self.navigationController pushViewController:vc animated:YES];
}

@end
